<?php
namespace App\Repositories\Eloquent;
use App\Models\Apply;
use App\Repositories\Contracts\ApplyRepository;
use Prettus\Repository\Eloquent\BaseRepository;
use Prettus\Repository\Criteria\RequestCriteria;
/**
 * Class ApplyRepositoryEloquent
 * @package namespace App\Repositories\Eloquent;
 */
class ApplyRepositoryEloquent extends BaseRepository implements ApplyRepository
{
    /**
     * Specify Model class name
     *
     * @return string
     */
    public function model()
    {
        return Apply::class;
    }

    /**
     * 查询权限并分页
     * @author lyndon
     * @date   2016-11-02T15:17:24+0800
     * @param $start
     * @param $length
     * @param $search
     * @param $order
     * @param array $cond
     * @param $all
     * @return array [type]
     */
    public function getApplyList($start, $length, $search, $order, $cond=[], $all)
    {
        $apply = $this->model;
        if ($search['value']) {
            if($search['regex'] == 'true'){
                $apply = $apply->where('name', 'like', "%{$search['value']}%");
            }else{
                $apply = $apply->where('name', $search['value']);
            }
        }
        if (!empty($cond)) {
            foreach ($cond as $key=>$value) {
                $apply = $apply->where($key, $value);
            }
        }

        $apply = $all ? $apply : $apply->notReview();

        $count = $apply->count();
        
        $apply = $apply->orderBy($order['name'], $order['dir']);

        $apply = $apply->offset($start)->limit($length)->get();

        return compact('count','apply');
    }
}
